package co.com.jaco.bl.aas.admin;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import co.com.jaco.dto.aas.PersonDTO;
import co.com.jaco.entity.aas.Credential;
import co.com.jaco.entity.aas.Person;
import co.com.jaco.entity.aas.User;

/**
 * Session Bean implementation class UserManagement
 */
@Stateless
@LocalBean
public class UserManagement implements UserManagementLocal {
	@PersistenceContext
	private EntityManager persistence;

	@Override
	public void createUser(PersonDTO user) {
		Person person = new Person();
		person.setFirstName(user.getFirstName());
		person.setMiddleName(user.getMiddleName());
		person.setFirstLastName(user.getFirstLastName());
		person.setSecondLastName(user.getSecondLastName());
		this.createPrincipal(person);
	}
	
	public void createPrincipal(Person principal) {
		persistence.persist(principal);
		User user = new User();
		user.setPerson(principal);
		persistence.persist(user);
		Credential credential = new Credential();
		credential.setUser(user);
		credential.setPassword(generatePassword(user).toString());
		persistence.persist(credential);

	}

	private StringBuilder generatePassword(User user) {
		StringBuilder password = new StringBuilder(Long.toString(System
				.currentTimeMillis()));
		return password;
	}

}
